home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / strlib.zip / STRFIELD.C < prev    next >
Text File  |  1993-01-04  |  3KB  |  72 lines

  1.  
  2. /*  File   : strfield.c
  3.     Author : Richard A. O'Keefe.
  4.     Updated: 21 April 1984
  5.     Defines: strfield()
  6.  
  7.     strfield(src, fields, chars, blanks, tabch)
  8.         is based on the key specifications of the sort(1) command.
  9.  
  10.         tabch corresponds to 'x' in -t'x'.  If it is NUL, a field
  11.         is leading layout (spaces, tabs &c) followed by at least
  12.         one non-layout character, and is terminated by the next
  13.         layout character or NUL.  If it is not NUL, a field is
  14.         terminated by tabch or NUL.
  15.  
  16.         fields is the number of fields to skip over.  It corresponds
  17.         to m in -m.n or +m.n .  There must be at least this many
  18.         fields, and only the last may be terminated by NUL.
  19.  
  20.         chars is the number of characters to skip after the fields
  21.         have been skipped.  At least this many non-NUL characters
  22.         must remain after the fields have been skipped.  Note that
  23.         it is entirely possible for this skip to cross one or more
  24.         field boundaries.  This corresponds to n in +m.n or -m.n .
  25.  
  26.         Finally, if blanks is not 0, any layout characters will be
  27.         skipped.  There need not be any.  This corresponds to the
  28.         letter b in +2.0b or -0.4b .
  29.  
  30.         The result is NullS if the source ran out of fields or ran
  31.         out of chars.  Otherwise it is a pointer to the first
  32.         character of src which was not skipped.  It is quite possible
  33.         for this character to be the terminating NUL.
  34.  
  35.     Example:
  36.         to skip to the user-id field of /etc/passwd:
  37.             user_id = strfield(line, 2, 0, 0, ':');
  38.  
  39.         to check whether "line" is at least 27 characters long:
  40.             if (strfield(line, 0, 27, 0, 0)) then-it-is;
  41.  
  42.         to select the third blank-delimited field in a line:
  43.             head = strfield(line, 2, 0, 1, 0);
  44.             tail = strfield(head, 1, 0, 0, 0);
  45.             (* the field is the tail-head characters starting at head *)
  46.  
  47.     It's not a bug, it's a feature: "layout" means any ASCII character
  48.         in the range '\1' .. ' ', including '\n', '\f' and so on.
  49. */
  50.  
  51. #include "strings.h"
  52.  
  53. char *strfield(src, fields, chars, blanks, tabch)
  54.     register char *src;
  55.     int fields, chars, blanks, tabch;
  56.     {
  57.         if (tabch <= 0) {
  58.             while (--fields >= 0) {
  59.                 while (*src <= ' ') if (!*src++) return NullS;
  60.                 while (*++src > ' ') ;
  61.             }
  62.         } else
  63.         if (fields > 0) {
  64.             do if (!*src) return NullS;
  65.             while (*src++ != tabch || --fields > 0);
  66.         }
  67.         while (--chars >= 0) if (!*src++) return NullS;
  68.         if (blanks) while (*src && *src++ <= ' ') ;
  69.         return src;
  70.     }
  71.  
  72.